#include "configfileclass.h"

ConfigFileClass::ConfigFileClass()
{
    //读取当前可执行文件目录
    QDir appDir = (QCoreApplication::applicationDirPath());
    m_dir = QDir(appDir).filePath("config/config.ini");

    ensureConfigFolderAndFile();
}


void ConfigFileClass::ensureConfigFolderAndFile() {
    //检查并创建 config 文件夹
    //QDir configDir("config");
    QDir appDir = (QCoreApplication::applicationDirPath());
    QString dir = QDir(appDir).filePath("config");
    QDir configDir(dir);
    if (!configDir.exists()) {
        if (configDir.mkpath(".")) {
            qDebug() << "Config folder created successfully.";
        } else {
            qDebug() << "Failed to create config folder!";
            return;
        }
    }

    //检查并创建 config.ini 文件
    QFile configFile(m_dir);
    if (!configFile.exists()) {
        if (configFile.open(QIODevice::WriteOnly)) {
            configFile.close();
            qDebug() << "config.ini created successfully.";
        } else {
            qDebug() << "Failed to create config.ini!";
        }
    } else {
        qDebug() << "config.ini already exists.";
    }
}


QVector<VIDEO_SAVE_CONFIG> ConfigFileClass::loadVideoConfigs() {
    QVector<VIDEO_SAVE_CONFIG> configs;
    QSettings settings(m_dir, QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    int size = settings.value("SaveNum/count", 8).toInt();

    for (int i = 0; i < size; ++i) {
        QString group = QString("SaveConfig_%1").arg(i);
        settings.beginGroup(group);

        VIDEO_SAVE_CONFIG config;
        config.resourcesName = settings.value("resourcesName", "default").toString();
        config.srcIP = settings.value("srcIp", "127.0.0.1").toString();
        config.bindPort = settings.value("bindPort", 1234).toUInt();
        config.bindIP = settings.value("bindIP", "127.0.0.1").toString();
        config.bindNetWorkCard = settings.value("bindNetWorkCard", "eth0").toString();
        config.bindWay = static_cast<UdpBindWay>(settings.value("bindWay", 1).toInt());
        config.des = settings.value("des", "des_").toString();
        config.saveMethod = static_cast<SaveIndex>(settings.value("saveMethod", 0).toInt());

        configs.append(config);
        settings.endGroup();
    }
    qDebug() << "Configs load to config.ini";
    return configs;
}

void ConfigFileClass::saveVideoConfigs(const QVector<VIDEO_SAVE_CONFIG>& configs) {
    QSettings settings(m_dir, QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    settings.setValue("SaveNum/count", configs.size());

    for (int i = 0; i < configs.size(); ++i) {
        VIDEO_SAVE_CONFIG config = configs[i];
        QString group = QString("SaveConfig_%1").arg(i); // 分组名称（如 WindowConfig_0）

        settings.beginGroup(group);
        settings.setValue("resourcesName", config.resourcesName);
        settings.setValue("srcIp", config.srcIP);
        settings.setValue("bindPort", config.bindPort);
        settings.setValue("bindIP", config.bindIP);
        settings.setValue("bindNetWorkCard", config.bindNetWorkCard);
        settings.setValue("bindWay", static_cast<int>(config.bindWay));
        settings.setValue("des", config.des);
        settings.setValue("saveMethod", static_cast<int>(config.saveMethod));
        settings.endGroup();
    }

    settings.sync(); // 强制写入磁盘
    qDebug() << "Configs saved to config.ini";
}

QStringList ConfigFileClass::loadDataSavePath() {
    QSettings settings(m_dir, QSettings::IniFormat);
    //先读存储的个数
    int pathCount = settings.value("SavePath/pathCount", 4).toInt();
    QStringList strList;
    for(int i = 0; i < pathCount; i++) {
        strList.append(settings.value(QString("SavePath/fileSavePath_%1").arg(i),
                                      QString("/mnt/disk%1").arg(i+1)).toString());
    }
    return strList;
}

HostMachine ConfigFileClass::loadHostMachine()
{
    HostMachine hostMachineConfig;
    QSettings settings(m_dir, QSettings::IniFormat);
    hostMachineConfig.listenIP = settings.value("HostMachine/listenIP", "127.0.0.1").toString();
    hostMachineConfig.listport = settings.value("HostMachine/listenPort", 8888).toUInt();
    hostMachineConfig.destIP = settings.value("HostMachine/destIP", "127.0.0.1").toString();
    hostMachineConfig.destPort = settings.value("HostMachine/destPort", 8888).toUInt();
    return hostMachineConfig;
}

void ConfigFileClass::saveHostMachine(const HostMachine &config)
{
    QSettings settings(m_dir, QSettings::IniFormat);
    settings.setValue("HostMachine/listenIP", config.listenIP);
    settings.setValue("HostMachine/listenPort", config.listport);
    settings.setValue("HostMachine/destIP", config.destIP);
    settings.setValue("HostMachine/destPort", config.destPort);
    settings.sync(); // 强制写入磁盘
}

MSGINI ConfigFileClass::loadMsgIni() const
{
    MSGINI msgIni;
    QSettings settings(m_dir, QSettings::IniFormat);
    msgIni._QxIP = settings.value("Messagecfg/QxIP", "127.0.0.1").toString();
    msgIni._QxPort = settings.value("Messagecfg/QxPort", 12110).toUInt();
    msgIni._TYFSIP = settings.value("Messagecfg/TYFSIP", "127.0.0.1").toString();
    msgIni._TYFSPort = settings.value("Messagecfg/TYFSPort", 12111).toUInt();
    msgIni._GNSSIP = settings.value("Messagecfg/GNSSIP", "127.0.0.1").toString();
    msgIni._GNSSPort = settings.value("Messagecfg/GNSSPort", 12112).toUInt();
    return msgIni;
}

void ConfigFileClass::saveDataSavePath(QStringList fileSavePaths) {
    QSettings settings(m_dir, QSettings::IniFormat);
    //先写存储的个数
    settings.setValue(QString("SavePath/pathCount"), fileSavePaths.count());
    for(int i = 0; i < fileSavePaths.count(); i++) {
        settings.setValue(QString("SavePath/fileSavePath_%1").arg(i), fileSavePaths[i]);
    }
    settings.sync(); // 强制写入磁盘
}


